package conexion;

import java.sql.*;

public class MySqlDBConn {
	/**
	 *Permite Cargar en Memoria los Drivers
         *
         *
         
	 */
	static {
                String driver = "com.mysql.jdbc.Driver";
		try {
			Class.forName(driver);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
        private Connection conn = getConnection();
	/**
	 * Obtiene una conexi�n a la Base de Datos.
	 */
	public Connection getConnection() {

		Connection connection = null;
                //String ip="coinsasgi.db.4146420.hostedresource.com";
                String ip="192.168.16.24";
                String db="coinsasgi";
                //String username = "coinsasgi";
                //String password = "C01ns4sg1";
                String username = "root";
                String password = "c01ns4";

                String url = "jdbc:mysql://"+ip+":3306/"+db+"?autoReconnect=true";
                
		try {
			connection =
			DriverManager.getConnection(url,username,password);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return connection;
	}
    public boolean isValidConnection() throws Exception {

        if (conn == null) { return false; }
        if (conn.isClosed()) { return false; }
        return testConnection("select 1 from dual");
    }
    public boolean testConnection( String query) {
        ResultSet rs = null;
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            if(stmt == null){ return false; }
            rs = stmt.executeQuery(query);
            if(rs == null){ return false; }
            if (rs.next()) { return true; }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            try {
              rs.close();
              stmt.close();
            }catch(Exception e) {
                // ignore
            }
        }
    }
	
}
